﻿<!DOCTYPE html>
<html>

<head>
    <meta charset="UTF-8">
    <title>Table</title>
    <link rel="stylesheet" href="/plugins/layui/css/layui.css" media="all"/>
    <link rel="stylesheet" href="/css/global.css" media="all">
    <link rel="stylesheet" href="/plugins/font-awesome/css/font-awesome.min.css">
    <link rel="stylesheet" href="/css/table.css"/>
    <style type="text/css">
    .searcheBottom{margin-bottom: 10px;}
    </style>
</head>

<body>
<div class="admin-main">
    <blockquote class="layui-elem-quote">
        <div class="layui-inline searcheBottom">
        <div class="layui-inline">
            <h3><b>上级行业：${parentName!}</b></h3>
            <span hidden id="parentId">${parentId}</span>
        </div>
        </div>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
        <div class="layui-inline" style="margin-bottom: 10px;">
            <div class="layui-inline">
                <input id="SEARCH_ID" class="layui-input" name="id" autocomplete="off" placeholder="请输入行业名称搜索"/>
            </div>
            </div>
        <div class="layui-inline searcheBottom" style="margin-bottom: 10px;">
            <button class="layui-btn" data-type="reload" id="search" >搜索</button>
            <button class="layui-btn layui-btn-primary" onclick="resetSearch()">重置</button>
            <a data-opt="add" href="/manage/industry/addChildIndustry?parentId=${parentId}" class="layui-btn">
                <i class="layui-icon">&#xe608;</i>添加子行业</a>
            <a href="javascript:history.back();" class="layui-btn layui-btn-primary">返回</a>    
        </div>
    </blockquote>
            <span style="font-size: 16px;"><b>子行业列表</b></span>
                <table class="layui-hide" id="tableID" lay-filter="tableFilter"></table>
</div>

<template id="editBoxTemplate" hidden>
    <div style="margin: 15px;">
        <fieldset class="layui-elem-field layui-field-title" style="margin-top: 20px;">
            <legend>行业编辑(<label class="layui-required-tip">标红的为必填项</label>)</legend>
        </fieldset>

        <form id="check-edit-form" class="layui-form js-form" action="/manage/industry/updataIndustry" method="post">
            <input type="hidden" name="id" class="layui-input js-id" />
            <div class="layui-form-item">
                <label class="layui-form-label layui-required-tip">行业名称</label>
                <div class="layui-input-block">
                    <input type="text" id="FORM_TITLE" name="industryName" lay-verify="required" autocomplete="off" placeholder="请输入行业名称" class="layui-input">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label layui-required-tip">排序</label>
                <div class="layui-input-block">
                    <input type="text" id="FORM_SORT" name="sort" required="" lay-verify="required|number|checkSort" autocomplete="off" placeholder="排序号" class="layui-input">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label layui-required-tip">简介</label>
                <div class="layui-input-block">
                    <textarea id="CONTENT" name="content" placeholder="简介" class="layui-textarea" lay-verify="required"></textarea>
                </div>
            </div>
            <div class="layui-form-item"style="display: none;">
                <label class="layui-form-label">状态</label>
                <div class="layui-input-block">
                    <input type="radio" class="js-status1" name="status" value="1"  title="启用" />
                    <input type="radio" class="js-status0" name="status" value="0" title="停用" />
                </div>
            </div>


            <button lay-filter="edit" lay-submit style="display: none;"></button>
        </form>
    </div>
    <script>
        layui.use(['form', 'layedit', 'laydate'], function() {
            var form = layui.form;
            form.render();
          //设置form自定义验证
            form.verify({
            	checkSort: function(value) {
            		var reg=/^[0-9]*$/; //含0正整数
                    if (!reg.test(value)) {
                            return "请输入纯数字";
                    }
                }
            });
        });
    </script>

</template>

<script type="text/javascript" src="/plugins/layui/layui.js"></script>
<script>
    layui.config({
        base: '/js/'
    });
    
    function resetSearch() {
        SEARCH_ID.value = '';
    }

    layui.use(['form', 'laydate','table','laydate','util'], function () {
    	var $ = layui.jquery,
        form = layui.form,
        table = layui.table,//表格
        laydate = layui.laydate,//日期
        util = layui.util;//工具类
        
        var id = parseInt(${parentId});
            
        pageInit();
        
      //执行一个 table 实例
        function pageInit(){
      	   table.render({
                 elem: '#tableID'
                 ,url:'/manage/industry/listChildIndustry'
                 ,where: {
                	parentId: id
                 }  
                 ,method: 'post'
                 ,cols: [[
                     {field:'id', title: 'ID',sort: true}
                     ,{field:'industryName', title: '行业名称'}
                     ,{field:'content', title: '行业简介'}
                     ,{field:'enableFlag', title: '状态 ', templet:function(d){
                    	  if(d.enableFlag==false){
                   		     return '<div class="layui-btn layui-btn-danger layui-btn-xs">已停用 </div>'
     		 	    	  }else{
     		 	    		 return '<div class="layui-btn layui-btn-xs">使用中</div>'
     		 	    	  }
                    	 
                     }}
                     ,{field:'updateTime', title: '修改时间', sort: true,templet: function(d){
                  	   if(null!=d.updateTime){
                  		   return util.toDateString(d.updateTime, 'yyyy-MM-dd HH:mm:ss');
                  	   }else{
                  		   return '';
                  	   }
                         
                     }}
                     ,{title: '操作',  align:'left',width:350 , templet:function(d){
   		 	    		  var str = '<a class="layui-btn layui-btn-xs" lay-event="edit">'
                              +'编辑</a>'
   		 	    		      +'<a href="/manage/industry/childIndustryList?parentId='+d.id+'&parentName='+d.industryName+'" class="layui-btn layui-btn-normal layui-btn-xs">'
   		                      +'子行业管理</a>'
   		                      +'<a href="/manage/industry/addChildIndustry?parentId='+d.id+'&parentName='+d.industryName+'" class="layui-btn  layui-btn-xs">'
   		                      +'添加子行业</a>';
   		                   if(d.enableFlag==true){
   		                	   str+='<a class="layui-btn layui-btn-xs  layui-btn-warm" lay-event="change">停用 </a>';
   		                   }else{
   		                	   str+='<a class="layui-btn layui-btn-xs" lay-event="change">启用</a>';
   		                   }
   		                   return str;
                     }}
                 ]]
                 ,page: true
             });
        }
            
        //监听工具条
        table.on('tool(tableFilter)', function(obj){ //注：tool是工具条事件名，test是table原始容器的属性 lay-filter="对应的值"
            var data = obj.data //获得当前行数据
                ,layEvent = obj.event; //获得 lay-event 对应的值
            var editBoxIndex = -1;
            if(layEvent === 'change'){
            	var id = data.id;
            	var enableFlag = data.enableFlag;
                //配置一个透明的询问框
                layer.confirm('确定要设置吗?', {
                    btn : [ '确定', '取消' ]//按钮
                }, function(index) {
                    $.ajax({
                        cache: true,
                        type: "GET",
                        url: "/manage/industry/updataIndustryStatus",
                        data: "id="+id+"&enableFlag="+enableFlag,
                        async: false,
                        error: function () {
                            layer.close(index);
                            alert("Connection error");
                        },
                        success: function (data) {
                            layer.close(index);
                            table.reload("tableID");
                        }
                    });
                });
            } else if(layEvent === 'edit'){
            	if (editBoxIndex !== -1) {
                    return;
                }
                var template = $('#editBoxTemplate').html();
                var ctx = $(this);
                editBoxIndex = layer.open({
                    type: 1,
                    title: '菜单编辑',
                    content: template,
                    btn: ['保存', '取消'],
                    area: ['800px', '80%'],
                    yes: function (index) {
                        //触发表单的提交事件
                        $('#layui-layer' + editBoxIndex).find('.js-form [lay-submit]').click();
                    },
                    full: function (elem) {
                        var win = window.top === window.self ? window : parent.window;
                        $(win).on('resize', function () {
                            var $this = $(this);
                            elem.width($this.width()).height($this.height()).css({
                                top: 0,
                                left: 0
                            });
                            elem.children('div.layui-layer-content').height($this.height() - 95);
                        });
                    },
                    success: function (layero, index) {
                        //弹出窗口成功后渲染表单
                        form.render();
                    },
                    end: function () {
                        editBoxIndex = -1;
                    }
                });

                layer.ready(function () {
                    var $layer = $('#layui-layer' + editBoxIndex);
                    //菜单ID
                    $layer.find('.js-id').val(data.id);
                    //标题
                    FORM_TITLE.value = data.industryName;
                    //设置排序号
                    FORM_SORT.value = data.sort;
                   //状态
                    var status = data.enableFlag==true?1:0;

                    CONTENT.value = data.content;
                    $layer.find('.js-status' + status).attr('checked', 'checked');
                    form.render();

                    form.on('submit(edit)', function (data) {
                        //这里可以写ajax方法提交表单
                        $.ajax({
                            cache: false,
                            type: "POST",
                            url: "/manage/industry/updataIndustry",
                            data: $("#check-edit-form").serialize(),
                            async: true,
                            error: function () {
                                alert("Connection error");
                            },
                            success: function (msg) {
                                pageInit();
                                layer.closeAll();
                            }
                        });
                        return false; //阻止表单跳转。如果需要表单跳转，去掉这段即可。
                    });
                })
            }
        });

        var $ = layui.$, active = {
                reload: function(){
                    //执行重载
                    table.reload('tableID', {
                        page: {
                            curr: 1 //重新从第 1 页开始
                        }
                        ,where: {
                            find : SEARCH_ID.value
                        }
                    });
                }
            };

            $('#search').on('click', function(){
                var type = $(this).data('type');
                active[type] ? active[type].call(this) : '';
            });

        $(".layui-input").keydown(function (e) {
            if(e.keyCode == 13){
                $('#search').click();
            }
        });

    });
</script>
</body>

</html>